<?php

session_start();
require_once '../libraryfiles/dbconfig.php';
$dbConn = mysql_connect ($dbhost, $dbuser, $dbpwd) or die ('MySQL connect failed. ' . mysql_error());
$errormsg='';
mysql_select_db($dbname) or die('Cannot select database. ' . mysql_error());
if(isset($_POST['btnLogin'])){

		$username	= $_POST['txtusername'];
		$pwd0	=	$_POST['txtpassword'];
		$pwd	=  $_POST['txtpassword'];
		$passwordlog = EnscryptPassword($pwd);
		$sql	= "SELECT * FROM tbluser WHERE username= '".$username."' AND user_pass = '$passwordlog' AND status=1 AND (user_type_id = 4 OR user_type_id = 1)";
		$result = mysql_query($sql);
		$row = mysql_fetch_assoc($result);
		//echo $user;
		if(mysql_num_rows($result)==0)
		{
			$errormsg="<label style='color:red;font-family:Tahoma, Arial, Verdana, Helvetica, sans-serif;
	font-size:12px;'>Username & Password are Invalid</label>";
		}
		else
		{
			$_SESSION['username'] = $username;
			$_SESSION['acc_id'] = $row['id'];
			$_SESSION['password'] = $pwd;
			$_SESSION['password0']=$pwd0;
			$_SESSION['usertype'] = $row['user_type_id'];
			$_SESSION['acc_type'] = $row['account_type_id'];
			$_SESSION['perm'] = $row['perm'];

			$sql_1 = "UPDATE tbluser SET last_visit = NOW(), acc_online='1', session_id = '" . session_id() . "' WHERE id = ".$row['id'];
			$result_1 = mysql_query($sql_1);

		if(isset($_POST['txtCheckRemember'])) {
			$month = time() + (60 * 60 * 24 * 30);
			setcookie('remember', $_POST['txtusername'], $month);
			setcookie('username', $_POST['txtusername'], $month);
			setcookie('password', $_POST['txtpassword'], $month);
		} elseif (!$_POST['txtCheckRemember']) {
			$past = time() - 100;
			if (isset($_COOKIE['remember'])) {
				setcookie('remember', '', $past);
			} if (isset($_COOKIE['username'])) {
				setcookie('username', '', $past);
			} if (isset($_COOKIE['password'])) {
				setcookie('password', '', $past);
			}
		}
			header("location:index.php");
			exit;
		}
}
function EnscryptPassword($passwordEnscript){
	return hash("sha512", md5(sha1(md5($passwordEnscript))));
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administrator</title>
    <link href="css/stylesheet.css" rel="stylesheet" />
    <script type="text/javascript" src="../templates/javascripts/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="templates/javascript/common.js"></script>
<?php
if(isset($include)){
	echo $include;
}
?>
</head>

<div id="content">
  <div class="box" style="width: 400px; min-height: 300px; margin-top: 100px; margin-left: auto; margin-right: auto;">
    <div class="heading">
      <h1><img src="images/lockscreen.png" alt="" />Please enter your login details</h1>
    </div>
    <div class="content" style="min-height: 150px; overflow: hidden;">
    <?php  if(!empty($errormsg)){?>
       <div class="warning">
	   		<center><?php echo $errormsg;?></center>
	   </div>
	   <?php }?>
      <form  method="post" name="frmLogin" id="frmLogin">
        <table style="width: 100%;">
          <tr>
              <td style="text-align: center;" rowspan="4"><img src="images/logo.png"  width="128" height="128" alt="Admin" /></td>
          </tr>
          <tr>
            <td>Username<br />
              <input value="<?php  echo isset($_COOKIE['username'])?$_COOKIE['username']:"";?>" name="txtusername" id="txtusername" title="Username" value="" size="20" maxlength="2048" style="margin-top: 4px;" />
              <br />
              <br />
              Password<br />
              <input value="<?php  echo isset($_COOKIE['password'])?$_COOKIE['password']:"";?>" name="txtpassword" id="txtpassword"  type="password" title="Password" value="" size="20" maxlength="2048" style="margin-top: 4px;" />

              <br /><br/>
              <a href="forget_password.php">Forget password</a>
              <br/><br/>
              <input <?php echo isset($_COOKIE['remember'])?"checked":"";?> type="checkbox" name="txtCheckRemember"> Remember Me <a href="#" style="margin-left:30px;"></a>

              </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td style="text-align: right;">
            <button type="submit" class="button" name="btnLogin">Login</button><button type="reset" class="btn" name="reset">Reset</button>
            </td>
          </tr>
        </table>
      </form>
  </div>
  </div>
</div>
